#coding:utf-8
# 矩阵求行列式

from Matrix import Matrix

def calcArr(M,i):
    rM = [ M.getRow(x)[1:] for x in range(0,M.row) if x != i ]
    sign = (-1) ** i

    return sign * M[i][0] * _det(Matrix(*rM))

def _det(this):
    if this.row != this.col:
        raise ValueError('只有方阵可以计算行列式!')

    # 懒得算，递归求解，这玩意儿性能一定糟透了
    if this.row == 1:
        return this[0][0]
    else:
        return sum([
           calcArr(this,r) for r in range(0,this.row)
        ])


def test():
    m_det = Matrix(
        [3,-1,-1,2],
        [-5,1,3,-4],
        [2,0,1,-1],
        [1,-5,3,-3])
    
    print(_det(m_det))
    print('30就对了')
    
if __name__=='__main__':
    import sys
    test()
